﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.ServiceModel.Syndication;

namespace NewsWorm {
    public class FeedItem : IArticle {
        internal FeedItem( SyndicationItem item, Feed parent ) {
            m_ParentFeed = parent;

            m_Url = item.Links[0].Uri.ToString();
            m_Title = item.Title.Text;

            //Fix for the title missing a proper frech accent
            if ( m_Title.Contains( "&#039;" ) ) {
                m_Title = m_Title.Replace( "&#039;", "'" );
            }

            if ( m_Title.Contains( "" ) ) {
                m_Title = m_Title.Replace( "&quot;", "\"" );
            }

            m_PubDate = item.PublishDate.ToString( "yyyy-MM-dd HH:mm" );

            if ( item.Summary != null )
                m_Summary = item.Summary.Text;
            else
                m_Summary = "No preview available.";

            //Feed item does not have PublishDate (e.g atom feed?)
            if ( m_PubDate.StartsWith( "000" ) )
                m_PubDate = item.LastUpdatedTime.ToString( "yyyy-MM-dd HH:mm" );
        }

        internal FeedItem(string url, string title, string pubdate, string summary,
            Feed parent) {
          
                this.m_Url = url;
                this.m_Title = title;
                this.m_PubDate = pubdate;
                this.m_Summary = summary;
                this.m_ParentFeed = parent;
        }

        private string m_Url;
        private string m_Title;
        private string m_PubDate;
        private string m_Summary;
        private Feed m_ParentFeed;

        public override bool Equals( object obj ) {
            FeedItem item = (FeedItem)obj;
            if ( item.Url.Equals( this.Url ) )
                return true;

            return false;
        }

        public override int GetHashCode() {
            return base.GetHashCode();
        }

        internal void SetParentFeed( Feed feed ) {
            this.m_ParentFeed = feed;
        }

        internal Feed ParentFeed {
            get { return m_ParentFeed; }
        }

        public IFeed Parent {
            get { return m_ParentFeed; }
        }

        public string ArticleUrl {
            get { return Url; }
        }

        internal string Url {
            get {
                return m_Url;
            }
            set {
                m_Url = value;
            }
        }

        public string ArticleTitle {
            get { return Title; }
        }

        internal string Title {
            get {
                return m_Title;
            }
            set {
                m_Title = value;
            }
        }
        internal string Published {
            get {
                return m_PubDate;
            }
            set {
                m_PubDate = value;
            }
        }

        public DateTime ArticlePublished {
            get {
                DateTime t = DateTime.Parse( this.Published );   
                return t; 
            }
        }

        public string ArticleSummary {
            get { return Summary; }
        }

        internal string Summary {
            get {
                return m_Summary;
            }
            set {
                m_Summary = value;
            }
        }
    }
}
